//
//  HomeViewController.m
//  QWwyNews
//
//  Created by Amisear on 15/10/9.
//  Copyright © 2015年 Amisear. All rights reserved.
//

#import "HomeViewController.h"
#import "ChannelModel.h"
#import "ChannelLable.h"

@interface HomeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (strong, nonatomic) IBOutlet UIScrollView *channelView;

@property (strong, nonatomic) IBOutlet UICollectionView *CollectionView;

@property (strong, nonatomic) IBOutlet UICollectionViewFlowLayout *CollectionLayout;

@property (strong, nonatomic) NSArray *channelList;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupChannel];
}

- (void)setupChannel {
    
    //设置scrollView缩进
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    // 遍历频道数组，添加 label
    CGFloat margin = 8.0;
    CGFloat x = margin;
    CGFloat h = self.channelView.bounds.size.height;
    
    for (ChannelModel *chan in self.channelList) {
        
        
        ChannelLable *l = [ChannelLable channelLabelWithTitle:chan.tname];
//        l.text = chan.tname;
        
//        [l sizeToFit];
        
        l.frame = CGRectMake(x, 0, l.bounds.size.width, h);
        x += l.bounds.size.width;
        
        [self.channelView addSubview:l];
    }
    
    self.channelView.contentSize = CGSizeMake(x + margin, h);
    
}

- (NSArray *)channelList {
    
    if (_channelList == nil) {
        _channelList = [ChannelModel ChannelList];
    }
    return _channelList;
}

- (void)viewDidLayoutSubviews {
    
    [self setupCollection];
    
}

- (void)setupCollection {
    
    self.CollectionLayout.itemSize = self.CollectionView.bounds.size;
    self.CollectionLayout.minimumLineSpacing = 0;
    self.CollectionLayout.minimumInteritemSpacing = 0;
    self.CollectionLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.CollectionView.pagingEnabled = YES;
    self.CollectionView.showsHorizontalScrollIndicator = NO;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.channelList.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ColectionCell" forIndexPath:indexPath];
    
    cell.backgroundColor = cell.backgroundColor = [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0) green:((float)arc4random_uniform(256) / 255.0) blue:((float)arc4random_uniform(256) / 255.0) alpha:1.0];
    
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
